 #This method reads the config file for the loading process.
 def read_load_file
        extract_options = String.new
        FasterCSV.foreach("load_config.csv") do |row|
        for field_counter in 0..row.length
              if row[field_counter] !="csv_files"
                    if field_counter == 0  
                          extract_options = extract_options+"#{row[field_counter]}"
                    else
                          extract_options = extract_options+",#{row[field_counter]}"	
                    end	
              end
        end	
    end	
    return extract_options	 
 end
 
 
 
 #This method returns the CSV files which are to be loaded into the table.
 def get_csv_file_list
      csv_file_list = String.new 
      FasterCSV.foreach("load_config.csv") do |row|
      for field_counter in 0..row.length
            if row[field_counter] == "csv_files"
                  for csv_file_counter in (field_counter+1)..row.length
                      if csv_file_counter == row.length
                            csv_file_list = csv_file_list+"#{row[csv_file_counter]}"
                      else
                            csv_file_list = csv_file_list+"#{row[csv_file_counter]},"
                      end	
                  end
                  return csv_file_list
                  break
            end	
      end
    end	
 end
 
 
  #This method checks the number of columns in a CSV file. This will be used while creating the dummy_headers.
  def get_number_of_columns_for_csv_file(filename)
        row_counter = 0
        @no_of_fields = []
        FasterCSV.foreach(filename) do |row|
                @no_of_fields[row_counter] = row.size
                row_counter+=1
        end
        return @no_of_fields.sort[@no_of_fields.size-1]	
  end



  #This is main method of data loading. This method decided if the loading is to be done for a single CSV file or multiple.
  def entry_method_for_process_to_start(filename)
          if filename.split(",").size > 1
                filename_arr = filename.split(",")
                for file_counter in 0..filename_arr.size-1
                      load_csv_to_table(filename_arr[file_counter])
                end
          else
                load_csv_to_table(filename)
          end	
  end	


  #This method sets all the empty values as nil so as to simplify the loading process.
  def set_blank_values_as_nil(filename)
          FasterCSV.foreach(filename) do |row|
          for field_counter in 0..row.length
                    if row[field_counter] == ' '
                          row[field_counter] = 'nil'
                    end	
          end
      end 
  end	 

 
  #This method has been now updated so that it can allow the creation of the tables without the preexisting the header files.
  def get_table_header_for_csv(filename)
          filename = filename.split(".")[0]
          header_filename = filename+"_header.csv"	
          header_fields = [] 
          if File.exist?(header_filename)
                  FasterCSV.foreach(header_filename) do |row|
                        for field_counter in 0..row.length
                              if row[field_counter] != nil
                                    header_fields << row[field_counter]
                              end	
                        end
                end
                return header_fields
          else
                  filename = filename+".csv"
                  if File.exists? filename
                        FasterCSV.foreach(filename) do |row|
                              for field_counter in 0..row.length
                                    header_fields << row[field_counter]
                              end
                              break
                        end	
                        set_blank_values_as_nil(filename)
                        total_columns = get_number_of_columns_for_csv_file(filename)
                        return header_fields
                  else
                        puts "The datasource CSV file is missing from the current working directory."
                        return false
                  end		
          end	
  end	 



  #This method checks if there is already a predefined structure file for the table.
  def structure_file_exists(table_name)
        if File.exist?("#{table_name}_structure.csv")
                return true
        else
                return false
        end	
 end



 #This is the actual load method which will be load the data to the table.
 def load_csv_to_table(filename)
        new_conn = create_new_connection
        @start_time_loading = Time.now
        execute_loading(filename,new_conn)	
 end



 def table_already_exists(new_conn,tablename)
        if new_conn.tables.size == 0  
              return false
        else
              new_conn.tables.each{|table|
                    puts table
                    puts table.class
                    if table.to_s == tablename
                            return true
                    else
                            return false
                    end	
              }
        end        
 end	 


 # This method is executing the partial columns loading.
 def execute_partial_column_loading(column_list,table_name,source_connection,destination_connection)
      actual_table_name = generate_missing_table_name(table_name,destination_connection)  
      p column_list
      for col in column_list  
          col_detail = col
          col_specs = get_the_col_spec_for_single_col(col,table_name,source_connection)  
          for col_info in col_specs
              col_detail << " "+col_info.to_s+","
          end
          col_detail[(col_detail.length-1)]=""
          if col != column_list[0] 
              #~ destination_connection.query("ALTER TABLE #{actual_table_name} ADD (#{col_detail})")
              destination_connection.execute("ALTER TABLE #{actual_table_name} ADD (#{col_detail})")
          else
              destination_connection.execute("CREATE TABLE #{actual_table_name}(#{col_detail})")
              #~ destination_connection.query("CREATE TABLE #{actual_table_name}(#{col_detail})")
          end      
      end      
 end
 
  
  # This method along with the method in Normalize:Splitter will be taken common in app_wide_methods.
  def generate_missing_table_name(table_name,connection)
        #~ all_tables = connection.query("show tables")
        all_tables = connection.tables
        for table in all_tables
              if table.to_s == table_name
                    table_name = table_name+"new"
              end  
        end
        return table_name  
  end
 
 
 
  def execute_loading(filename,new_conn)	
        row_counter = 0 
        filename = filename.gsub(/,/,'')
        tablename = filename.gsub(/\.csv/,'')
        tablename = tablename.gsub(/\,/,'')
        if !table_already_exists(new_conn,tablename)
                tables_header = get_table_header_for_csv(filename)
                if tables_header
                      for column in 0..tables_header.length-1
                            if tables_header[column] != nil
                                  if structure_file_exists(tablename)
                                        column_details = get_the_col_specs(tables_header[column],tablename)
                                  else
                                        # HERE RATHER THAN ALLOCATING VARCHAR 255 BY DEFAULT; 
                                        # GENERATE THE MOST SUITABLE SIZE AND DATATYPE OPTION
                                        column_details = "varchar(255)"
                                  end		
                                  if column == 0 
                                        #~ new_conn.query("CREATE TABLE #{tablename}(#{tables_header[column]} #{column_details})")
                                        new_conn.execute("CREATE TABLE #{tablename}(#{tables_header[column]} #{column_details})")
                                  else
                                        new_conn.execute("ALTER TABLE #{tablename} ADD `#{tables_header[column]}` #{column_details}")
                                        #~ new_conn.query("ALTER TABLE #{tablename} ADD `#{tables_header[column]}` #{column_details}")
                                  end
                            end	
                      end	
                      filename = tablename+".csv"
                      FasterCSV.foreach(filename) do |row|
                              @connected_row=String.new
                              if row.size!=0
                                    for field in row
                                            @connected_row << "\"#{field}\","
                                    end
                                    puts 
                                    @connected_row[@connected_row.length-1]=''
                                    puts @connected_row
                                    #~ new_conn.query("Insert into #{tablename} values(#{@connected_row})")
                                    new_conn.execute("Insert into #{tablename} values(#{@connected_row})")
                              end
                              row_counter = row_counter + 1
                      end	
              end	 
        else
              puts "The table is already exists in the database. Please rename the CSV file"	
        end			
  end